package com.example.side_pull_menu.Fragment;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.side_pull_menu.Fragment_9kj;
import com.example.side_pull_menu.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Fragment_9 extends Fragment{
    private Spinner spinner;
    private Button bu_query;
    private TextView textView;
    private TextView tv_id;
    private TextView tv_carno;
    private TextView recharge;
    private TextView tv_pepole;
    private int j=0;
    private TextView tv_time;
    private ListView lv;
    private Context mContext;
    private List<Fragment_9kj>fragment_9kjs=new ArrayList<>();
    MyAdapterr myAdapterr = new MyAdapterr();
    public Fragment_9() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_9, container, false);
        initView(view);
        initSQl();
        initData();
        lv.setAdapter(myAdapterr);
        bu_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myAdapterr.notifyDataSetInvalidated();
            }
        });
        return view;
    }
    private void initSQl(){
        SQLiteDatabase db=getActivity().openOrCreateDatabase("info.db", getActivity().MODE_PRIVATE, null);
        Cursor infol=db.query("info",null, null,null, null, null, null);
        int i=0;
        if (infol!=null){
            while (infol.moveToNext()){
                Fragment_9kj data=new Fragment_9kj();
                i++;
                data.setId(i+"");
                data.setTime(infol.getString(infol.getColumnIndex("time")));
                data.setCarno(infol.getString(infol.getColumnIndex("carno")));
                data.setName(infol.getString(infol.getColumnIndex("name")));
                data.setAmount(infol.getString(infol.getColumnIndex("amount")));
                fragment_9kjs.add(data);
        }
        }
        if(i==0) {
            textView.setText("暂无历史记录");
        }
    }
    private void initData(){
        final String[] sspinner=getResources().getStringArray(R.array.time);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,sspinner);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, final int i, long l) {
                j=i;
            switch (j){
                case 0:
                    Collections.sort(fragment_9kjs,new sort2());
                    break;
                case 1:
                    Collections.sort(fragment_9kjs,new sort1());
                    break;
            }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
    private void initView(View view) {
        spinner = (Spinner) view.findViewById(R.id.spinner);
        bu_query = (Button) view.findViewById(R.id.bu_query);
        tv_id = (TextView) view.findViewById(R.id.tv_id);
        textView=(TextView)view.findViewById(R.id.textView);
        tv_carno = (TextView) view.findViewById(R.id.tv_carno);
        recharge = (TextView) view.findViewById(R.id.recharge);
        tv_pepole = (TextView) view.findViewById(R.id.tv_pepole);
        tv_time = (TextView) view.findViewById(R.id.tv_time);
        lv = (ListView) view.findViewById(R.id.lv);
        bu_query.setOnClickListener((View.OnClickListener) mContext);
    }
    class MyAdapterr extends BaseAdapter {
        @Override
        public int getCount() {
            return fragment_9kjs.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.fragmnt_9_1,null);
            TextView id=view1.findViewById(R.id.tv_id);
            TextView carno=view1.findViewById(R.id.tv_carno);
            TextView recharge=view1.findViewById(R.id.recharge);
            TextView people=view1.findViewById(R.id.tv_pepole);
            TextView time=view1.findViewById(R.id.tv_time);
            id.setText(fragment_9kjs.get(i).getId()+"");
            carno.setText(fragment_9kjs.get(i).getCarno()+"");
            recharge.setText(fragment_9kjs.get(i).getAmount()+"");
            people.setText(fragment_9kjs.get(i).getName()+"");
            time.setText(fragment_9kjs.get(i).getTime()+"");
            return view1;
        }
    }
    public class sort1 implements Comparator<Fragment_9kj> {
        @Override
        public int compare(Fragment_9kj kj9, Fragment_9kj kj91) {
            return kj9.getTime().compareTo(kj91.getTime());
        }
    }
    public class sort2 implements Comparator<Fragment_9kj> {
        @Override
        public int compare(Fragment_9kj kj9, Fragment_9kj kj91) {
            return kj91.getTime().compareTo(kj9.getTime());
        }
    }
}
